package com.learn.algorithm.graph;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * https://www.luogu.com.cn/problem/P3366
 */
public class Prim {

    public static int n;
    public static int m;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            n = (int) in.nval;
            in.nextToken();
            m = (int) in.nval;

            List<List<int[]>> graph = new ArrayList<>();
            for (int i = 0; i < n + 1; i++) {
                graph.add(new ArrayList<>());
            }
            for (int i = 0; i < m; i++) {
                in.nextToken();
                int from = (int) in.nval;
                in.nextToken();
                int to = (int) in.nval;
                in.nextToken();
                int weight = (int) in.nval;
                graph.get(from).add(new int[]{to, weight});
                graph.get(to).add(new int[]{from, weight});
            }
            PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
            heap.addAll(graph.get(1));
            boolean[] set = new boolean[n + 1];
            set[1] = true;
            int nodeCount = 1;
            int ans = 0;
            while (!heap.isEmpty()) {
                int[] edge = heap.poll();
                int to = edge[0];
                int cost = edge[1];
                if (!set[to]) {
                    nodeCount++;
                    ans += cost;
                    set[to] = true;
                    heap.addAll(graph.get(to));
                }
            }
            out.println(nodeCount == n ? ans : "orz");
        }
        out.flush();
        out.close();
        br.close();
    }
}
